
import org.ccnx.ccn.impl.support.Log;


public class CCNFileSharerServer {
	String rootPath = "";
	String dataPath = "";
	
	public CCNFileSharerServer(String rootPath)
	{
		this.rootPath = rootPath;
		this.dataPath = rootPath + ".LS-INFO/";
		init();
	}
	protected void init()
	{
		FileIndexer indexer = new FileIndexer(rootPath, dataPath);
		indexer.indexFiles("~");
	}

	public void listen(String uri)
	{
		try {
			CCNFileProxy proxy = new CCNFileProxy(rootPath, uri);
			// All we need to do now is wait until interrupted.
			proxy.start();
			while (!proxy.finished()) {
				// we really want to wait until someone ^C's us.
				try {
					Thread.sleep(100000);
				} catch (InterruptedException e) {
					// do nothing
				}
			}
		} catch (Exception e) {
			Log.warning("Exception in ccnFileProxy: type: " + e.getClass().getName() + ", message:  "+ e.getMessage());
			Log.warningStackTrace(e);
			System.err.println("Exception in ccnFileProxy: type: " + e.getClass().getName() + ", message:  "+ e.getMessage());
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		String defaultRootPath	= "/home/zym/ftp_root/"; 
		String defaultURI		= "ccnx:/test_ftp";
		/*
		if (args.length < 2) {
			usage();
			return;
		}
		String filePrefix = args[0];
		String ccnURI = (args.length > 1) ? args[1] : defaultURI;
		*/
		String filePrefix = defaultRootPath;
		String ccnURI = defaultURI;
		CCNFileSharerServer server = new CCNFileSharerServer(filePrefix);
		server.listen(ccnURI);
	}
//	private static String usage(){
//		return " CCNFtpServer <FilePath> <CCNURI>";
//	}

}
